package cn.innosmart.aq.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.innosmart.aq.util.SystemConstant;

/**
 * Created by David on 2015/8/20.
 */
public class SceneBean implements Parcelable {
    public static final Creator<SceneBean> CREATOR = new Creator<SceneBean>() {
        public SceneBean createFromParcel(Parcel source) {
            return new SceneBean(source);
        }
        public SceneBean[] newArray(int size) {
            return new SceneBean[size];
        }
    };
    public int pns_priority;
    private String iconName;
    private String title;
    private ArrayList<ActionBean> commands;
    private String id;
    private String place;
    private String pns;

    public SceneBean(JSONObject jsonObject) {
        try {
            this.id = jsonObject.getString("id");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            this.iconName = jsonObject.getString("icon_name");
        } catch (JSONException e) {
            e.printStackTrace();
            this.iconName = "icon_scene";
        }
        try {
            this.place = jsonObject.getString("place");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.title = jsonObject.getString("title");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.pns_priority = jsonObject.getInt("pns_priority");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.pns = (String) jsonObject.getJSONArray("enabled_notifications").get(0);
        } catch (JSONException e) {
//            e.printStackTrace();
        }
        JSONArray jsonCommmands = new JSONArray();
        JSONObject jsonCommand = null;
        ActionBean actionBean = null;
        commands = new ArrayList<ActionBean>();
        try {
            jsonCommmands = jsonObject.getJSONArray("commands");
            for (int i = 0; i < jsonCommmands.length(); i++) {
                jsonCommand = jsonCommmands.getJSONObject(i);
                actionBean = new ActionBean();
                actionBean.setValue(jsonCommand.getDouble("value"));
                try {
                    actionBean.setAddress(jsonCommand.getString("address"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                actionBean.setOrder(jsonCommand.getInt("order"));
                actionBean.setField(jsonCommand.getString("field"));
                commands.add(actionBean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public SceneBean() {

    }

    protected SceneBean(Parcel in) {
        this.iconName = in.readString();
        this.title = in.readString();
        this.commands = in.createTypedArrayList(ActionBean.CREATOR);
        this.id = in.readString();
    }

    /**
     * g根据场景的ID查询场景对象
     * @param scenId
     * @return
     */
    public static SceneBean queryScene(String scenId) {
        ArrayList<SceneBean> sceneBeans = SystemConstant.currentAquariumBean.getSceneList();
        for (int i = 0; i < sceneBeans.size(); i++) {
            if (sceneBeans.get(i).getId().equals(scenId)) {
                return sceneBeans.get(i);
            }
        }
        return null;
    }

    public String getPns() {
        return pns;
    }

    public void setPns(String pns) {
        this.pns = pns;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getIconName() {
        return iconName;
    }

    public void setIconName(String iconName) {
        this.iconName = iconName;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public ArrayList<ActionBean> getCommands() {
        return commands;
    }

    public void setCommands(ArrayList<ActionBean> commands) {
        this.commands = commands;
    }

    public String toDeleteParams() {
        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("id", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonParams.toString();
    }

    public String toParams() {
        JSONObject jsonParams = new JSONObject();
        JSONArray jsonCommands = new JSONArray();
        JSONObject jsonCommand = null;
        try {
            jsonParams.put("title", title);
            if (id != null)
                jsonParams.put("id", id);
            jsonParams.put("icon_name", "icon_scene");
            jsonParams.put("place", place);
            jsonParams.put("pns_priority", pns_priority);
            try {
                if (!TextUtils.isEmpty(pns))
                    jsonParams.put("enabled_notifications", new JSONArray().put("pns"));
                else
                    jsonParams.put("enabled_notifications", new JSONArray());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            for (ActionBean actionBean : commands) {
                jsonCommand = new JSONObject();
                jsonCommand.put("order", actionBean.getOrder());
                jsonCommand.put("value", actionBean.getValue());
                jsonCommand.put("field", actionBean.getField());
                if (actionBean.getAddress() != null) {
                    jsonCommand.put("address", actionBean.getAddress());
                }
                jsonCommands.put(jsonCommand);
            }
            jsonParams.put("commands", jsonCommands);
            return jsonParams.toString();
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.iconName);
        dest.writeString(this.title);
        dest.writeTypedList(commands);
        dest.writeString(this.id);
    }

}
